package com.be.beadmin.service.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.service.dto.input.PrintDesignItemDto;
import com.be.beadmin.service.dto.input.PrintDesignItemQueryCriteria;
import com.be.beadmin.service.dto.output.PrintDesignItemEx;
import com.be.beadmin.service.service.PrintDesignItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author chenwei
* @date 2023-05-10
**/
@RestController
public class PrintDesignItemController {

    @Autowired
    private  PrintDesignItemService printDesignItemService;


    @PostMapping(value = "/service/getPrintDesignItemByPage")
    public ResultModel getPrintDesignItemByPage(@RequestBody PrintDesignItemQueryCriteria pageEntity)  {
        return ResultModel.success(printDesignItemService.getPrintDesignItemByPage(pageEntity)) ;
    }

    @PostMapping("/service/addPrintDesignItem")
    public ResultModel addPrintDesignItem(@RequestBody PrintDesignItemDto printDesignItemDto){
        printDesignItemService.addPrintDesignItem(printDesignItemDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/service/delPrintDesignItem")
    public ResultModel delPrintDesignItem(@RequestBody PrintDesignItemDto printDesignItemDto){
        printDesignItemService.delPrintDesignItem(printDesignItemDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/service/updatePrintDesignItem")
    public ResultModel updatePrintDesignItem(@RequestBody PrintDesignItemDto printDesignItemDto){
        printDesignItemService.updatePrintDesignItem(printDesignItemDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/service/getDesignFileDetailById")
    public ResultModel getDetailById(@RequestBody String id){
        printDesignItemService.getDetailById(id);
        return ResultModel.success(printDesignItemService.getDetailById(id));
    }


    @PostMapping("/service/exportPrintDesignItem")
    public void exportPrintDesignItem(HttpServletResponse response ,@RequestBody PrintDesignItemQueryCriteria pageEntity) throws IOException {
        Page<PrintDesignItemEx> printDesignItemPage = printDesignItemService.getPrintDesignItemByPage(pageEntity);
        List<PrintDesignItemEx> printDesignItemExes = printDesignItemPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PrintDesignItem接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PrintDesignItemEx.class).sheet("sheet1").doWrite(printDesignItemExes);
    }
}